package com.maycari.captainbi.scheduled;

import com.maycari.captainbi.data.dto.SyncRecordDto;
import com.maycari.captainbi.data.po.SyncRecordPo;
import com.maycari.captainbi.listener.SyncDto;
import com.maycari.captainbi.listener.SyncListener;
import com.maycari.captainbi.service.AuthorizationManagementService;
import com.maycari.captainbi.service.SyncRecordService;
import com.maycari.captainbi.util.MaycariRedis;
import com.maycari.tool.jackson.JsonUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author lsm
 * @version 1.0
 * @date 2021/4/13 8:53
 */
@Component
@Slf4j
public class SyncRecordExceptionHandlingTask {

    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private SyncRecordService syncRecordService;

    @XxlJob("syncRecordExceptionHandlingTask")
    public ReturnT<String> execute(String param) {

        log.info("同步所有错误或者异常的记录》》》》》》》》》");

        //查询所有错误或者异常的记录
        List<SyncRecordDto> syncRecordDtos = syncRecordService.getAllExceptionRecord();
        log.info("syncRecordDtos.size():.......{}",syncRecordDtos.size());
        //循环放入消息队列
        syncRecordDtos.stream().forEach(dto -> {

            log.info("getParams:     》》》》》   " + dto.getParams());
            SyncDto syncDto = JsonUtil.parse(dto.getParams(), SyncDto.class);
            syncDto.setSyncRecordId(dto.getId());
            log.info("syncDto: >>>: " + syncDto.toString());

            jmsTemplate.convertAndSend(SyncListener.SYNC_LISTENER, syncDto);
        });
        log.info("#####################同步所有错误或者异常的记录END###################");
        return ReturnT.SUCCESS;
    }
}
